﻿
namespace SistemaLogistica.Windows.Forms.Administracion
{
    using System;
    using System.Windows.Forms;
    using EDOSwit.Controls.Helpers;
    using SistemaLogistica.Business.Logic;
    using SistemaLogistica.Windows.Helpers;

    public delegate void DelegadoSeleccionar(object sender, int idSucursal);

    public partial class Ubigeo : UserControl
    {
        public event DelegadoSeleccionar seleccionarSucursal;
        
        public Ubigeo()
        {
            InitializeComponent();
        }

        public void InitialLoad()
        {
            try
            {
                this.cmbDepartamento.DisplayMember = "Departamento";
                this.cmbDepartamento.ValueMember = "IdDepartamento";

                this.cmbProvincia.DisplayMember = "Provincia";
                this.cmbProvincia.ValueMember = "IdProvincia";

                this.cmbDistrito.DisplayMember = "Distrito";
                this.cmbDistrito.ValueMember = "IdDistrito";

                this.cmbSucursal.DisplayMember = "Direccion";
                this.cmbSucursal.ValueMember = "IdSucursal";

                this.cmbDepartamento.DataSource = UbigeoManager.Instancia.ListarDepartamentos();
            }
            catch (Exception ex)
            {
                Mensaje.ShowMessageAlert(this.ParentForm, Constantes.TituloMensaje, ex.Message);
            }
        }

        private void cmbDepartamento_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbDepartamento.SelectedIndex != -1)
            {
                this.cmbProvincia.DataSource = UbigeoManager.Instancia.ListarProvincias(Convert.ToInt32(cmbDepartamento.SelectedValue));
                this.cmbProvincia.Enabled = true;
                cmbProvincia_SelectedIndexChanged(sender, EventArgs.Empty);
            }
        }

        private void cmbProvincia_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbDepartamento.SelectedIndex != -1)
            {
                this.cmbDistrito.DataSource = UbigeoManager.Instancia.ListarDistritos(Convert.ToInt32(this.cmbDepartamento.SelectedValue), Convert.ToInt32(this.cmbProvincia.SelectedValue));
                this.cmbDistrito.Enabled = true;
                cmbDistrito_SelectedIndexChanged(sender, EventArgs.Empty);
            }
        }

        private void cmbDistrito_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbDistrito.SelectedIndex != -1)
            {
                int ubigeoID = UbigeoManager.Instancia.ListarIDPorDPD(Convert.ToInt32(this.cmbDepartamento.SelectedValue), Convert.ToInt32(this.cmbProvincia.SelectedValue), Convert.ToInt32(this.cmbDistrito.SelectedValue));
                this.cmbSucursal.DataSource = (SucursalManager.Instancia.ListarPorUbigeo(ubigeoID));
                this.cmbSucursal.Enabled = true;
                cmbSucursal_SelectedIndexChanged(sender, EventArgs.Empty);
            }
        }

        private void cmbSucursal_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.seleccionarSucursal != null)
            {
                this.seleccionarSucursal(sender, Convert.ToInt32(this.cmbSucursal.SelectedValue));
            }
        }

        private void Focus_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                e.Handled = true;
                SendKeys.Send("{TAB}");
            }
        }

        private void Ubigeo_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                InitialLoad();
            } 
        }
    }
}
